<template>
  <div class="container">
    <div class="content">
      <div class="content-inner">
        <img class="logo-image" alt="logo" :src="logo" />
        <a-tabs default-active-key="1">
          <a-tab-pane key="1" title="密码激活">
            <a-form
              :model="pswForm"
              class="login-form"
              layout="vertical"
              @submit="handleSubmit"
            >
              <a-form-item
                field="password"
                :rules="[{ required: true, message: '密码必填' }]"
                :validate-trigger="['change', 'blur']"
                hide-label
              >
                <a-input-password
                  v-model="pswForm.password"
                  placeholder="请输入密码"
                  allow-clear
                >
                  <template #prefix>
                    <icon-user />
                  </template>
                </a-input-password>
              </a-form-item>
              <a-form-item
                field="confirmpwd"
                :rules="[{ required: true, message: '确认密码必填' }]"
                :validate-trigger="['change', 'blur']"
                hide-label
              >
                <a-input-password
                  v-model="pswForm.confirmpwd"
                  placeholder="确认密码"
                  allow-clear
                >
                  <template #prefix>
                    <icon-lock />
                  </template>
                </a-input-password>
              </a-form-item>
              <a-space :size="16" direction="vertical">
                <a-button
                  type="primary"
                  html-type="submit"
                  long
                  :loading="loading"
                >
                  确认
                </a-button>
              </a-space>
            </a-form>
          </a-tab-pane>
        </a-tabs>
      </div>
      <div class="footer">
        <Footer />
      </div>
      <a-modal
        v-model:visible="visible"
        title="用户激活成功"
        :width="500"
        :unmount-on-close="true"
        ok-text="前往登录"
        @ok="handleOk"
        @cancel="handleCancel"
      >
        <p
          ><span>您的账号已激活，账号名称为：</span
          ><span style="font-weight: bold">{{ userName }}</span
          ><icon-copy v-if="userName" class="copy-icon" @click="copy"
        /></p>
        <p><span>密码为您刚刚设置的密码。</span></p>
      </a-modal>
    </div>
  </div>
</template>

<script lang="ts" setup>
  import { reactive, ref } from 'vue';
  import { ValidatedError } from '@arco-design/web-vue/es/form/interface';
  import Footer from '@/components/footer/index.vue';
  import logo from '@/assets/images/logo.png';
  import useLoading from '@/hooks/loading';
  import { useRoute, useRouter } from 'vue-router';
  import Message from '@arco-design/web-vue/es/message';
  import { setUserPwd } from '@/api/user';
  import pwdEncrypt from '@/utils/encryption/pwd';
  import useClipboard from 'vue-clipboard3';

  const { loading, setLoading } = useLoading();
  const router = useRouter();
  const route = useRoute();
  const userid = (route.query?.userid as string) || '';

  const pswForm = reactive({
    password: '',
    confirmpwd: '',
  });

  const userName = ref('');
  const visible = ref(false);
  const { toClipboard } = useClipboard();
  const copy = async () => {
    try {
      await toClipboard(userName.value);
      Message.success('复制成功');
    } catch (err) {
      Message.error('复制失败');
    }
  };
  const handleOk = () => {
    // 前往登录页面
    router.push('/login');
    visible.value = false;
  };
  const handleCancel = () => {
    visible.value = false;
  };

  const handleSubmit = async ({
    errors,
    values,
  }: {
    errors: Record<string, ValidatedError> | undefined;
    values: Record<string, any>;
  }) => {
    if (loading.value) return;
    if (!errors) {
      if (!userid) {
        Message.error('链接错误');
      }
      if (values.password !== values.confirmpwd) {
        Message.error('密码不一致');
        return;
      }
      setLoading(true);
      try {
        const pwd = pwdEncrypt(values.password);
        const result = await setUserPwd(userid, pwd);
        if (result.status) {
          visible.value = true;
          userName.value = result.data;
        }
      } catch (err) {
        if (typeof err === 'string') {
          Message.error(err);
        }
      } finally {
        setLoading(false);
      }
    }
  };
</script>

<style lang="less" scoped>
  .container {
    display: flex;
    height: 100vh;

    .content {
      position: relative;
      display: flex;
      flex: 1;
      align-items: center;
      justify-content: center;
      padding-bottom: 40px;
      background: url('@/assets/images/active-user-bg.png') no-repeat;
    }

    .content-inner {
      padding: 56px 67px 75px;
      width: 494px;
      background: #ffffff;
      border-radius: 12px;
    }

    .footer {
      position: absolute;
      right: 0;
      bottom: 0;
      width: 100%;
    }
  }

  .logo-image {
    display: inline-block;
    width: 360px;
    height: 100px;
  }
  .copy-icon {
    cursor: pointer;
    margin-left: 8px;
    color: rgb(var(--primary-6));
  }
</style>
